package com.hong.spring.config;

import lombok.extern.log4j.Log4j2;
import org.apache.rocketmq.client.exception.MQClientException;
import org.apache.rocketmq.client.producer.DefaultMQProducer;

/**
 * @author: csh
 * @Date: 2021/3/16 09:43
 * @Description:rocketmq生产者
 */
@Log4j2
public class Producer{
    //生产实例
    private DefaultMQProducer defaultMQProducer;
    //生产组
    private String producerGroup;
    //地址
    private String namesrvAddr;

    /**
     * 初始化
     */
    public void init() throws MQClientException {
        // 参数信息
        log.info("DefaultMQProducer 初始化");
        log.info(producerGroup);
        log.info(namesrvAddr);
        // 初始化
        defaultMQProducer = new DefaultMQProducer(producerGroup);
        defaultMQProducer.setNamesrvAddr(namesrvAddr);
        defaultMQProducer.setInstanceName(String.valueOf(System.currentTimeMillis()));
        defaultMQProducer.start();
        log.info("rocketmq start success!");
    }


    public void destroy() {
        defaultMQProducer.shutdown();
    }

    public DefaultMQProducer getDefaultMQProducer() {
        return defaultMQProducer;
    }

    public void setProducerGroup(String producerGroup) {
        this.producerGroup = producerGroup;
    }

    public void setNamesrvAddr(String namesrvAddr) {
        this.namesrvAddr = namesrvAddr;
    }

}
